package com.imut.lagain.controller;

import com.imut.lagain.service.IFinalPasswordService;
import com.imut.lagain.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.HashMap;
import java.util.Map;

/**
 * 最终答案之锁控制器
 */

@RestController
@RequestMapping("/final")
@RequiredArgsConstructor
@Validated
public class FinalAnswerController {
    private static final Logger log = LoggerFactory.getLogger(FinalAnswerController.class);
    
    private final IFinalPasswordService finalPasswordService;
    private final JwtUtil jwtUtil;
    
    /**
     * 验证最终密码
     * @param authorization JWT令牌
     * @param request 密码验证请求
     * @return 验证结果
     */
    @PostMapping("/verify")
    public ResponseEntity<Map<String, Object>> verifyPassword(
            @RequestHeader("Authorization") @NotBlank String authorization,
            @Valid @RequestBody PasswordVerifyRequest request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String token = authorization.replace("Bearer ", "");
            if (!jwtUtil.validateToken(token)) {
                response.put("success", false);
                response.put("message", "Token无效");
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
            }
            
            Long userId = jwtUtil.getUserIdFromToken(token);
            if (userId == null) {
                response.put("success", false);
                response.put("message", "Token无效");
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
            }
            
            boolean isValid = finalPasswordService.validatePassword(request.getPassword());
            if (isValid) {
                response.put("success", true);
                response.put("message", "密码验证成功");
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "密码验证失败");
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
            }
        } catch (Exception e) {
            log.error("验证最终密码失败", e);
            response.put("success", false);
            response.put("message", "验证密码失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 密码验证请求DTO
     */
    public static class PasswordVerifyRequest {
        @NotBlank(message = "密码不能为空")
        @Size(min = 1, max = 100, message = "密码长度必须在1-100字符之间")
        private String password;
        
        public String getPassword() {
            return password;
        }
        
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    /**
     * 更新密码请求DTO
     */
    public static class UpdatePasswordRequest {
        @NotBlank(message = "管理员密钥不能为空")
        @Size(min = 1, max = 100, message = "管理员密钥长度必须在1-100字符之间")
        private String adminKey;
        
        @NotBlank(message = "新密码不能为空")
        @Size(min = 1, max = 100, message = "新密码长度必须在1-100字符之间")
        private String newPassword;
        
        public String getAdminKey() {
            return adminKey;
        }
        
        public void setAdminKey(String adminKey) {
            this.adminKey = adminKey;
        }
        
        public String getNewPassword() {
            return newPassword;
        }
        
        public void setNewPassword(String newPassword) {
            this.newPassword = newPassword;
        }
    }
}

